package com.yanqu.road.server.manger.specialserver;

import com.yanqu.road.entity.config.system.NormalWeightItem;
import com.yanqu.road.entity.specialserver.IceServerBurnConfig;
import com.yanqu.road.entity.specialserver.IceServerStoveLevelConfig;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.helper.WeightHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class IceServerMgr extends TempMgr {

    /**
     * 熔炉等级配置
     */
    private static Map<Integer, IceServerStoveLevelConfig> stoveLevelConfigMap = new ConcurrentHashMap<>();

    /**
     * 燃烧物配置
     */
    private static Map<Integer, IceServerBurnConfig> burnConfigMap = new ConcurrentHashMap<>();

    /**
     * 煤炭数量
     */
    private static List<NormalWeightItem> coalList = new ArrayList<>();

    /**
     * 最大熔炉等级
     */
    private static int maxStoveLevel;

    /**
     * 默认燃烧物
     */
    private static int initStoveItem;

    /**
     * npc数量
     */
    public static final int MAX_NPC_NUM = 10;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {

        Map<Integer, IceServerStoveLevelConfig> tmpStoveLevelConfigMap = new ConcurrentHashMap<>();
        List<String> clist = StringUtils.stringToStringList(GameConfig.ICE_SERVER_FURNACE, "\\|");
        for (int i = 0; i < clist.size(); i++) {
            int level = i + 1;
            String itemStr = clist.get(i);
            List<String> iList = StringUtils.stringToStringList(itemStr, ";");
            IceServerStoveLevelConfig stoveLevelConfig = new IceServerStoveLevelConfig();
            stoveLevelConfig.setLevel(level);
            stoveLevelConfig.setCoalNum(Integer.parseInt(iList.get(0)));
            stoveLevelConfig.setNeedExp(Long.parseLong(iList.get(1)));
            stoveLevelConfig.setTouchAddExp(Integer.parseInt(iList.get(2)));
            tmpStoveLevelConfigMap.put(level, stoveLevelConfig);

            maxStoveLevel = Math.max(maxStoveLevel, stoveLevelConfig.getLevel());
        }
        stoveLevelConfigMap = tmpStoveLevelConfigMap;

        Map<Integer, IceServerBurnConfig> tmpBurnConfigMap = new ConcurrentHashMap<>();
        List<String> blist = StringUtils.stringToStringList(GameConfig.ICE_SERVER_BURN, "\\|");
        for (String bItem : blist) {
            List<String> iList = StringUtils.stringToStringList(bItem, ";");
            IceServerBurnConfig burnConfig = new IceServerBurnConfig();
            burnConfig.setBurnId(Integer.parseInt(iList.get(0)));
            burnConfig.setSilver(Long.parseLong(iList.get(1)));
            burnConfig.setNeedExp(Long.parseLong(iList.get(2)));
            tmpBurnConfigMap.put(burnConfig.getBurnId(), burnConfig);

            if (burnConfig.getNeedExp() == 0) {
                initStoveItem = burnConfig.getBurnId();
            }
        }
        burnConfigMap = tmpBurnConfigMap;

        List<NormalWeightItem> tmpCoalList = new ArrayList<>();
        List<String> nlist = StringUtils.stringToStringList(GameConfig.ICE_SERVER_NEED, "\\|");
        for (String nItem : nlist) {
            List<String> iList = StringUtils.stringToStringList(nItem, ";");
            tmpCoalList.add(new NormalWeightItem(new BigInteger(iList.get(0)), Integer.parseInt(iList.get(1))));
        }
        coalList = tmpCoalList;
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static Map<Integer, IceServerStoveLevelConfig> getStoveLevelConfigMap() {
        return stoveLevelConfigMap;
    }

    public static Map<Integer, IceServerBurnConfig> getBurnConfigMap() {
        return burnConfigMap;
    }

    public static IceServerBurnConfig getBurnConfig(int burnId) {
        return burnConfigMap.get(burnId);
    }

    public static int getMaxStoveLevel() {
        return maxStoveLevel;
    }

    public static int getInitStoveItem() {
        return initStoveItem;
    }

    public static IceServerStoveLevelConfig getStoveLevelConfig(int level) {
        return stoveLevelConfigMap.get(level);
    }

    /**
     * 随机煤炭数量
     */
    public static int getRandomCoalNum() {
        NormalWeightItem randomResult = WeightHelper.getRandomResult(coalList);
        if (randomResult == null) {
            return 0;
        }
        return randomResult.getValue().intValue();
    }
}
